package com.zqs.service.impl;

import com.zqs.entity.Collect;
import com.zqs.entity.Product;
import com.zqs.mapper.CollectMapper;
import com.zqs.mapper.ProductMapper;
import com.zqs.service.CollectService;
import com.zqs.service.ex.InsertException;
import com.zqs.service.ex.ProductNothingnessException;
import com.zqs.service.ex.UpdateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    private CollectMapper collectMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public void addProductInCollect(Integer pid,Integer uid) {
        Product product = productMapper.findByPid(pid);
        Collect collect = new Collect();
        collect.setUid(uid);
        collect.setTitle(product.getTitle());
        collect.setPrice(product.getPrice());
        collect.setImage(product.getImage());
        collect.setDetail(product.getDetail());
        collect.setState(1);
        Integer row = collectMapper.addCollect(collect);
        if (row != 1){
            throw new InsertException("插入时发生未知异常!");
        }
    }

    @Override
    public List<Collect> getUidCollect(Integer uid) {
        List<Collect> collectList = collectMapper.getByUid(uid);
        if (collectList == null){
            throw new ProductNothingnessException("商品没有找到!");
        }
        return collectList;
    }

    @Override
    public void updateStateById(Integer id,Integer uid) {
        Integer row = collectMapper.updateStateById(id,uid);
        if (row != 1){
            throw new UpdateException("修改时发生异常!");
        }
    }
}
